package org.glow.changgou.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.goods.dao.CategoryMapper;
import org.glow.changgou.goods.pojo.Category;
import org.glow.changgou.goods.pojo.Para;
import org.glow.changgou.goods.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author Glow
 * @Date 2022-09-22 21:26:39
 * @Description
 * @Version 1.0
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {
    @Autowired
    private CategoryService categoryService;
    @Override
    public Page<Category> findPage(Category category, int page, int size) {
        Page<Category> pageInfo = new Page<>(page,size);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,category);

        return categoryService.page(pageInfo,queryWrapper);
    }

    @Override
    public Page<Category> findPage(int page, int size) {
        Page<Category> pageInfo = new Page<>(page,size);
        return categoryService.page(pageInfo);
    }

    @Override
    public List<Category> findList(Category category) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,category);
        return categoryService.list(queryWrapper);
    }

    @Override
    public void delete(Integer id) {
             categoryService.removeById(id);
    }

    @Override
    public void update(Category category) {
        categoryService.update(category);
    }

    @Override
    public void add(Category category) {
            categoryService.save(category);
    }

    @Override
    public Category findById(Integer id) {
        return categoryService.getById(id);
    }

    @Override
    public List<Category> findAll() {
        return categoryService.list();
    }

    @Override
    public List<Category> findByParentId(Integer pid) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId,pid);
        return  categoryService.list(queryWrapper);
    }

    public LambdaQueryWrapper<Category> getCondition(LambdaQueryWrapper<Category> queryWrapper, Category category){
//        Example example=new Example(Para.class);
//        Example.Criteria criteria = example.createCriteria();
        if(category!=null) {
            // id
            if (!StringUtils.isEmpty(category.getId())) {
//                criteria.andEqualTo("id",para.getId());
                queryWrapper.eq(Category::getId, category.getId());
            }
            // 分类名称
            if (!StringUtils.isEmpty(category.getName())) {
//                criteria.andLike("name", "%" + category.getName() + "%");
                    queryWrapper.like(Category::getName,category.getName());
            }
            // 商品数量
            if (!StringUtils.isEmpty(category.getGoodsNum())) {
//                criteria.andEqualTo("goodsNum", category.getGoodsNum());
                    queryWrapper.eq(Category::getGoodsNum,category.getGoodsNum());
            }
            // 是否显示
            if (!StringUtils.isEmpty(category.getIsShow())) {
//                criteria.andEqualTo("isShow", category.getIsShow());
                    queryWrapper.eq(Category::getIsShow,category.getIsShow());
            }
            // 是否导航
            if (!StringUtils.isEmpty(category.getIsMenu())) {
//                criteria.andEqualTo("isMenu", category.getIsMenu());
                    queryWrapper.eq(Category::getIsMenu,category.getIsMenu());
            }
            // 排序
            if (!StringUtils.isEmpty(category.getSeq())) {
//                criteria.andEqualTo("seq", category.getSeq());
                    queryWrapper.eq(Category::getSeq,category.getSeq());
            }
            // 上级ID
            if (!StringUtils.isEmpty(category.getParentId())) {
//                criteria.andEqualTo("parentId", category.getParentId());
                queryWrapper.eq(Category::getParentId,category.getParentId());
            }

            // 模板ID
            if (!StringUtils.isEmpty(category.getTemplateId())) {
//                criteria.andEqualTo("templateId", category.getTemplateId());
                queryWrapper.eq(Category::getTemplateId,category.getTemplateId());
            }
        }
        return queryWrapper;
    }
}
